package com.hugeyurt.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.CharsetUtils;
import org.apache.http.util.EntityUtils;

public final class HttpComponentUtil 
{
	public static final int cache = 10 * 1024; 
	public static String doPostByJson(String url, String json){
		 
	    CloseableHttpClient httpclient = HttpClientBuilder.create().build();
	    HttpPost post = new HttpPost(url);
	    String result = null;
	    try {
	        StringEntity s = new StringEntity(json,"UTF-8");
	        s.setContentEncoding("UTF-8");
	        s.setContentType("application/json");
	        post.setEntity(s);
	        HttpResponse res = httpclient.execute(post);
	        if(res.getStatusLine().getStatusCode() == org.apache.http.HttpStatus.SC_OK){
	            result = EntityUtils.toString(res.getEntity());

	        }
	    } catch (Exception e) {
	        throw new RuntimeException(e);
	    }
	    return result;
	}
	
	
	
	 public static String get(String url) throws IOException {
		    String result="";
			CloseableHttpClient httpclient = HttpClients.createDefault();
			try {
				HttpGet httpGet = new HttpGet(url);
			   CloseableHttpResponse response = httpclient.execute(httpGet);
				try { System.out.println(response.getStatusLine());
					HttpEntity entity = response.getEntity();
					result=EntityUtils.toString(entity);
				} finally {
					response.close();
				}
			} catch (ClientProtocolException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
				httpclient.close();
			}
            return result;

		}



	    public static void download(String url, String filepath) {  
	        try {  
	        	CloseableHttpClient httpclient = HttpClients.createDefault(); 
	            HttpGet httpget = new HttpGet(url);  
	            HttpResponse response = httpclient.execute(httpget);  
	  
	            HttpEntity entity = response.getEntity();  
	            InputStream is = entity.getContent();
	            
	            if (filepath == null) return ; 
	                
	            File file = new File(filepath);  
	            FileOutputStream fileout = new FileOutputStream(file);  
	            /** 
	             * ����ʵ������Ч�� ���û�������С 
	             */  
	            byte[] buffer=new byte[cache];  
	            int ch = 0;  
	            while ((ch = is.read(buffer)) != -1) {  
	                fileout.write(buffer,0,ch);  
	            }  
	            is.close();  
	            fileout.flush();  
	            fileout.close();  
	  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  

	    }  
	  

	 
}
